package com.aiyige.page.my.order.refund;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.my.customer.adapter.RefundReasonDialogListAdapter;
import com.aiyige.page.my.order.model.RefundReasonModel;
import com.aiyige.utils.MaxHeightRecyclerView;
import com.aiyige.utils.UIHelper;
import com.aiyige.utils.widget.BaseBottomDialogFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 选择退款理由
 */
public class SelectRefundReasonDialog extends BaseBottomDialogFragment {

    @BindView(R.id.recyclerview)
    MaxHeightRecyclerView recyclerView;

    @BindView(R.id.cancelBtn)
    TextView cancelBtn;

    Unbinder unbinder;

    RefundReasonDialogListAdapter adapter;

    List<RefundReasonModel> list;

    RefundReasonModel selected;

    public static SelectRefundReasonDialog newInstance() {
        return new SelectRefundReasonDialog();
    }

    Listener listener;

    public void setLisenter(Listener callback) {
        this.listener = callback;
    }

    public void setData(List<RefundReasonModel> list) {
        this.list = list;
    }


    public void setSelected(RefundReasonModel selected) {
        this.selected = selected;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_customer_type, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        if (selected != null && !TextUtils.isEmpty(selected.getId())) {
            for (RefundReasonModel refundReasonModel : list) {
                if (selected.getId().equals(refundReasonModel.getId())) {
                    refundReasonModel.setSelected(true);
                    break;
                }
            }
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        int height = metric.heightPixels;   // 屏幕高度（像素）
        recyclerView.setMaxHeight((height/2)- UIHelper.dip2px(50));
        adapter = new RefundReasonDialogListAdapter();
        adapter.bindToRecyclerView(recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < list.size(); ++i) {
                    list.get(i).setSelected(i == position);
                }
                adapter.notifyDataSetChanged();
                if (listener != null) {
                    listener.onSelect(list.get(position));
                }
                dismiss();
            }
        });
        adapter.setNewData(list);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public interface Listener {
        void onSelect(RefundReasonModel value);
    }
}
